home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / progwin.zip / COLORS2.ZIP / COLORS2.C < prev    next >
C/C++ Source or Header  |  1991-10-22  |  4KB  |  134 lines

  1.  /*---------------------------------------------------------
  2.      COLORS2.C -- Version Using Modeless Dialog Box Version
  3.           (C) Charles Petzold, 1990
  4.    --------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. long FAR PASCAL WndProc     (HWND, WORD, WORD, LONG);
  9. BOOL FAR PASCAL ColorScrDlg (HWND, WORD, WORD, LONG);
  10.  
  11. HWND hDlgModeless;
  12.  
  13. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  14.                  LPSTR lpszCmdLine, int nCmdShow)
  15.      {
  16.      static char szAppName[] = "Colors2";
  17.      HWND        hwnd;
  18.      MSG        msg;
  19.      WNDCLASS   wndclass;
  20.  
  21.      if (hPrevInstance)
  22.          return FALSE;
  23.  
  24.      wndclass.style        = CS_HREDRAW | CS_VREDRAW;
  25.      wndclass.lpfnWndProc    = WndProc;
  26.      wndclass.cbClsExtra    = 0;
  27.      wndclass.cbWndExtra    = 0;
  28.      wndclass.hInstance    = hInstance;
  29.      wndclass.hIcon        = NULL;
  30.      wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  31.      wndclass.hbrBackground    = CreateSolidBrush (0L);
  32.      wndclass.lpszMenuName    = NULL;
  33.      wndclass.lpszClassName    = szAppName;
  34.  
  35.      RegisterClass (&wndclass);
  36.  
  37.      hwnd = CreateWindow (szAppName, "Color Scroll",
  38.                     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  39.                     CW_USEDEFAULT, CW_USEDEFAULT,
  40.                     CW_USEDEFAULT, CW_USEDEFAULT,
  41.                     NULL, NULL, hInstance, NULL);
  42.  
  43.      ShowWindow (hwnd, nCmdShow);
  44.      UpdateWindow (hwnd);
  45.  
  46.      hDlgModeless = CreateDialog (hInstance, "ColorScrDlg", hwnd,
  47.                     MakeProcInstance (ColorScrDlg, hInstance));
  48.  
  49.      while (GetMessage (&msg, NULL, 0, 0))
  50.         {
  51.         if (hDlgModeless == 0 || !IsDialogMessage (hDlgModeless, &msg))
  52.              {
  53.              TranslateMessage (&msg);
  54.              DispatchMessage (&msg);
  55.              }
  56.         }
  57.      return msg.wParam;
  58.      }
  59.  
  60. BOOL FAR PASCAL ColorScrDlg (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  61.      {
  62.      static short color[3];
  63.      HWND        hwndParent, hCtrl;
  64.      short      nCtrlID, nIndex;
  65.  
  66.      switch (message)
  67.         {
  68.         case WM_INITDIALOG:
  69.              for (nCtrlID = 10; nCtrlID < 13; nCtrlID++)
  70.                 {
  71.                 hCtrl = GetDlgItem (hDlg, nCtrlID);
  72.                 SetScrollRange (hCtrl, SB_CTL, 0, 255, FALSE);
  73.                 SetScrollPos   (hCtrl, SB_CTL, 0, FALSE);
  74.                 }
  75.              return TRUE;
  76.  
  77.         case WM_VSCROLL:
  78.              hCtrl = HIWORD (lParam);
  79.              nCtrlID = GetWindowWord (hCtrl, GWW_ID);
  80.              nIndex = nCtrlID - 10;
  81.              hwndParent = GetParent (hDlg);
  82.  
  83.              switch (wParam)
  84.                 {
  85.                 case SB_PAGEDOWN:
  86.                    color [nIndex] += 15;      // fall through
  87.                 case SB_LINEDOWN:
  88.                    color [nIndex] = min (255, color [nIndex] + 1);
  89.  
  90.                    break;
  91.                 case SB_PAGEUP:
  92.                    color [nIndex] -= 15;       // fall through
  93.                 case SB_LINEUP:
  94.                    color [nIndex] = max (0, color [nIndex] - 1);
  95.                    break;
  96.                 case SB_TOP:
  97.                    color [nIndex] = 0;
  98.                    break;
  99.                 case SB_BOTTOM:
  100.                    color [nIndex] = 255;
  101.                    break;
  102.                 case SB_THUMBPOSITION:
  103.                 case SB_THUMBTRACK:
  104.                    color [nIndex] = LOWORD (lParam);
  105.                    break;
  106.                 default:
  107.                    return FALSE;
  108.                 }
  109.              SetScrollPos (hCtrl, SB_CTL,      color [nIndex], TRUE);
  110.              SetDlgItemInt (hDlg, nCtrlID + 3, color [nIndex], FALSE);
  111.  
  112.              DeleteObject (GetClassWord (hwndParent, GCW_HBRBACKGROUND));
  113.              SetClassWord (hwndParent, GCW_HBRBACKGROUND,
  114.                 CreateSolidBrush (RGB (color [0], color [1], color [2])));
  115.  
  116.              InvalidateRect (hwndParent, NULL, TRUE);
  117.              return TRUE;
  118.         }
  119.      return FALSE;
  120.      }
  121.  
  122. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  123.      {
  124.      switch (message)
  125.         {
  126.         case WM_DESTROY:
  127.              DeleteObject (GetClassWord (hwnd, GCW_HBRBACKGROUND));
  128.              PostQuitMessage (0);
  129.              return 0;
  130.         }
  131.      return DefWindowProc (hwnd, message, wParam, lParam);
  132.      }
  133.  
  134.